package org.example;

import org.example.pojo.Goods;
import org.example.pojo.Person;
import org.example.pojo.Student;
import org.example.pojo.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Springtest {
    private ApplicationContext context;
    @Before
    public void setUp() throws Exception {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void test() {
        org.example.pojo.Test tests = (org.example.pojo.Test) context.getBean("test");
        System.out.println(tests);
    }

    @Test
    public void test1() {
        User user = (User) context.getBean("user");
        user.fun();
        System.out.println(user);
    }

    @Test
    public void test2() {
        Student stu = (Student) context.getBean("stu");
        System.out.println(stu);
    }

    @Test
    public void test3() {
        Person person = (Person) context.getBean("person");
        System.out.println(person);
    }

    @Test
    public void test4() {
        // byName自动注入
        // 根据id匹配 goodstype
        Goods gd = (Goods) context.getBean("gd");
        System.out.println(gd);
    }

    @Test
    public void test5() {
        // byType 自动注入
        // 根据class匹配 GoodsType
        // 若有两个则报错
        Goods gd2 = (Goods) context.getBean("gd2");
        System.out.println(gd2);
    }

    @Test
    public void test6() {
        // 默认是单例模式
        Goods gd = (Goods) context.getBean("gd2");
        Goods gd2 = (Goods) context.getBean("gd2");
        System.out.println(gd==gd2);

    }

}
